#########################################################################################
# Title: Appendix_A.R
# Author: Yuki Atsusaka (atsusaka@rice.edu)
# Aim: Code to replicate results in Online Appendix A of Atsusaka (2021)
#########################################################################################

# CLEAN THE GLOBAL ENVIRONMENT AND READ PACKAGES
rm(list=ls())
library(viridis)


pdf(here::here("Figure_A1.pdf"), width=5, height=8) # Opena pdf file
par(mfrow=c(3,2), mar=c(5,2,2,1), oma=c(0,2,0,0)) 

gr <- rev(magma(10))
line <- c(1,1,1,1,2,1,1,1,1,1)
M = seq(from=0, to=100, by=1) 
tauM.vec = c(60,50,50,40,40,30)
tauW.vec = c(50,60,40,50,30,40)


for(t in 1:6){
tauM = tauM.vec[t] # VARYING MINORITY TURNOUT WHILE FIXING WHITE TURNOUT
tauW = tauW.vec[t]
print(c(tauM, tauW))


# REPLICATE PANEL B OF FIGURE 2 with Different "tauM" and "tauW"
plot(0, type="n", xaxt="n", ylim=c(0,1),xlim=c(0,100),
     ylab="",xlab="M (Racial Margin of Victory)", cex.lab=1)
axis(1, at = seq(0, 100, by=25), las=1)
abline(v=50,  lty=2, lwd=1, col="gray60")
abline(h=0.5, lty=2, lwd=1, col="gray60")

for(i in 1:10){                                 # We explore C(0,100) 
  C = i * 10                                    # Varying Raw % Minority Voters
  C.prime = C*tauM/(C*tauM + (100-C)*tauW)*100  # Turnout-Adjusted % Minority Voters
  MC.sqrt.min50 = sqrt(M*C.prime) - 50          # GEOMETRIC MEAN
  P  <- pnorm(MC.sqrt.min50, mean=0, sd=1)      # Probability of Entry
  lines(P ~ M, col=gr[i], lwd=3, lty=line[i])
}         

title(paste0("Minority ", tauM, "%", " v. ", "White ", tauW, "% (Turnout)"), 
      adj=0, cex.main=1)

}

dev.off() # Close the file

#########################################################################################
# END OF THIS R SOURCE FILE
#########################################################################################